//
//  YJWWordCell.m
//  百思不得骑姐
//
//  Created by YJW on 16/12/5.
//  Copyright © 2016年 yijiang. All rights reserved.
//

#import "YJWWordCell.h"
#import "YJWWordModel.h"
#import <UIImageView+WebCache.h>

@interface YJWWordCell ()

// 头像
@property (weak, nonatomic) IBOutlet UIImageView *headView;
// 昵称
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
// 发布时间
@property (weak, nonatomic) IBOutlet UILabel *time1Label;
// 发布多长时间
@property (weak, nonatomic) IBOutlet UILabel *time2Label;

// 顶
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
// 踩
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
// 转发
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
// 评论
@property (weak, nonatomic) IBOutlet UIButton *commentButton;

@end

@implementation YJWWordCell

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}

-(void)setModel:(YJWWordModel *)model
{
    _model = model;
    
    //设置数据
    [self.headView sd_setImageWithURL:[NSURL URLWithString:model.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.nameLabel.text = model.name;
    self.time1Label.text = model.created_at;
    [self time2Label:self.time2Label withFromDate:model.created_at];
    
    //底部系列按钮
    [self setBtnTitle:self.dingButton withCount:model.ding withPlaceHolder:@"点赞"];
    [self setBtnTitle:self.caiButton withCount:model.cai withPlaceHolder:@"踩"];
    [self setBtnTitle:_shareButton withCount:model.repost withPlaceHolder:@"分享"];
    [self setBtnTitle:self.commentButton withCount:model.comment withPlaceHolder:@"评论"];
    
    
}

-(void)time2Label:(UILabel *)time2Label withFromDate:(NSString *)fromdate
{
    //获取当前时间
    NSDate *current = [NSDate date];
    //时间格式化
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH-mm-ss";
    NSDate *date = [format dateFromString:fromdate];
    //获取时间比较的结果
    NSDateComponents *components = [current deltaFrom:date];
    time2Label.text = [NSString stringWithFormat:@"%zd时%ld分前",components.hour,components.minute];
}

-(void)setBtnTitle:(UIButton *)button withCount:(NSInteger)count withPlaceHolder:(NSString *)placeHolder
{
    NSString *title;
    if (count == 0) {
        title = placeHolder;
    }else if (count >= 10000) {
        title = [NSString stringWithFormat:@"%.1f万",count / 10000.0];
    } else {
        title = [NSString stringWithFormat:@"%zd",count];
    }
    [button setTitle:title forState:UIControlStateNormal];
}


/**
 设置 cell 的 frame
 */
-(void)setFrame:(CGRect)frame
{
    CGFloat margin = 10;
    frame.origin.x = margin;
    frame.size.width -= 2 * frame.origin.x;
    frame.size.height -= margin;
    frame.origin.y += margin;
    
    [super setFrame:frame];
}


/**
 将字符 年-月-日 时-分-秒 转化为：秒、分、时...
 */
-(void)testTime
{
    NSDate *current = [NSDate date];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH-mm-ss";
    NSDate *date = [format dateFromString:@"2016-12-5 17:50:30"];
    
    NSTimeInterval second = [current timeIntervalSinceDate:date];
    YJWLog(@"%f",second);
}

@end
